package com.project.JunM.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.JunM.entity.SGather;
import com.project.JunM.entity.SGatherDetails;
import com.project.JunM.entity.SPay;
import com.project.JunM.entity.SPayDetails;
import com.project.JunM.mapper.SPayDetailsMapper;
import com.project.JunM.mapper.SPayMapper;
import com.project.JunM.service.SPayService;
import com.project.common.entity.PageVo;
import com.project.common.utils.Serial;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SPayImpl implements SPayService {
    @Autowired
    private SPayMapper sPayMapper;

    @Autowired
    private SPayDetailsMapper sPayDetailsMapper;

    @Override
    public int insertSelective(SPay record, String sPayDetailsObj) throws IOException {

        //出库编号
        Serial serial = new Serial();
        serial.addUser("payId","403");
        String payId = serial.getNumber("payId", "403", null, null, null);

        record.setPayId(payId);
        //出库申请单 默认值给 S001-0 为等待审核
        record.setCheckTag("S001-0");
        //出库申请单 库存标志
        //K002-0: 未登记未调度
        record.setStoreTag("K002-0");
        sPayMapper.insertSelective(record);
        //获取出库申请单（SGather）中的主键
        Integer id = record.getId();
        System.out.println(id);
        //将json数据转成Java对象（SGatherDetails）
        SPayDetails[] arr = JSONObject.parseObject(sPayDetailsObj, SPayDetails[].class);
        //循环存入数据库中
        for (int i=0;i<arr.length;i++){
            SPayDetails sPayDetails = arr[i];
            sPayDetails.setParentId(id);
            sPayDetailsMapper.insertSelective(sPayDetails);
        }
        return 1;
    }

    @Override
    public int insertSelective(SPay record) throws IOException {
        Serial serial = new Serial();
        serial.addUser("payId","403");
        String payId = serial.getNumber("payId", "403", null, null, null);
        record.setPayId(payId);
        return sPayMapper.insertSelective(record);
    }

    @Override
    public PageVo<SPay> queryRegisterCheckAll(Map<String, String> map) {

        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<SPay> sPayList = sPayMapper.queryRegisterCheckAll();
        PageInfo pageInfo = new PageInfo(sPayList);

        PageVo pageVo = new PageVo();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public PageVo<SPay> queryRegisterSelectAll(Map<String, String> map) {
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<SPay> sPayList = sPayMapper.queryRegisterSelectAll();
        PageInfo pageInfo = new PageInfo(sPayList);

        PageVo pageVo = new PageVo();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public PageVo<SPay> queryOutSetAll(Map<String, String> map, List<String> productIdList) {
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<SPay> sPayList = sPayMapper.queryOutSetAll();

        for (int i=0;i<sPayList.size();i++){
            SPay sPay = sPayList.get(i);
            sPay.setProductId(productIdList.get(i));
        }
        PageInfo pageInfo = new PageInfo(sPayList);

        PageVo pageVo = new PageVo();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public List<String> queryOutSetAll1() {
        return sPayMapper.queryOutSetAll1();
    }

    @Override
    public PageVo<SPay> queryOutCheckInAll(Map<String, String> map) {
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<SPay> sPayList = sPayMapper.queryOutCheckInAll();
        PageInfo pageInfo = new PageInfo(sPayList);

        PageVo pageVo = new PageVo();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public PageVo<SPay> queryOutCheckInAuditAll(Map<String, String> map) {
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<SPay> sPayList = sPayMapper.queryOutCheckInAuditAll();
        PageInfo pageInfo = new PageInfo(sPayList);

        PageVo pageVo = new PageVo();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public int updateSPayByPayId(SPay sPay) {
        return sPayMapper.updateSPayByPayId(sPay);
    }

    @Override
    public int updateByPrimaryKeySelective(SPay record) {
        return sPayMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<SPay> queryAllSPay() {
        return sPayMapper.queryAllSPay();
    }

    @Override
    public PageVo<SPay> queryOutCheckInSelectAll(Map<String, String> map) {
        Integer pageNum = Integer.valueOf(map.get("pageNum"));
        Integer pageSize= Integer.valueOf(map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);
        List<SPay> sPayList = sPayMapper.queryOutCheckInSelectAll();
        PageInfo pageInfo = new PageInfo(sPayList);

        PageVo pageVo = new PageVo();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }


}
